$(document).ready(function() {
	var ordersCancledModel = function(orders) {
		var self = this;
		self.orders = ko.mapping.fromJS(orders);
		self.orderId = ko.observable('');
		self.status = ko.observable('准备就绪');
		
		self.orderDate = function(timeMill) {
			if (timeMill == '') {
				return '';
			} else {
				return new Date(timeMill).format('yyyy-MM-dd      hh:mm:ss');
			}
		};
		
		self.getCancledOrderById = function() {
			var orderId = self.orderId();
			if ( parseInt(orderId) >= 0) {
				$.getJSON('uncancled/order/' + orderId, function(response) {
					var orders = [];
					if (response.success == true && response.order) {
						orders.push(response.order);
						self.status('正在编辑作废订单');
					} else {
						orders.push(blankOrder);
						self.status('找不到id为' + orderId +'的订单或者订单不是作废状态');
					}
					ko.mapping.fromJS(orders, self.orders);
				});
			}
		};
		self.getCancledOrdersToday = function() {
			$.getJSON('uncanceled/orders/today', function(response) {
				var orders = [];
				if (response.success == true) {
					orders = response.orders;
					self.status('获取今天作废的订单');
				} else {
					orders.push(blankOrder);
					self.status('获取作废订单操作失败');
				}
				ko.mapping.fromJS(orders, self.orders);
			});
		};
		self.uncancledOrder = function(order) {
			var orderId = order.id();
			if (parseInt(orderId) > 0) {
				$.get('uncancled/order/' + orderId + '?_method=PUT', function(response) {
					if (response.success == true) {
						self.orders.remove(order);
						self.status('恢复订单成功');
					} else {
						self.status('恢复订单失败');
					}
				});
			};
		};
	};
	
	var blankCancledOrders = [];
	blankCancledOrders.push(blankOrder);
	var cancledModel = new ordersCancledModel(blankCancledOrders);
	ko.applyBindings(cancledModel, document.getElementById('tabs-uncancled'));
});